'use strict';

define(['controllers', 'codemirror', 'codemirror-sql'], function (controllers, CodeMirror) {
    controllers.system.register('SqlController', ['$scope', '$http', function ($scope, $http) {
        $scope.init = function () {
            $scope.running = false;
            $scope.runSuccess = true;
            setTimeout(function () {
                $scope.sql_editor = CodeMirror.fromTextArea(document.getElementById("sql_editor"), {
                    mode: "text/x-mysql",
                    styleActiveLine: true,
                    lineNumbers: true,
                    lineWrapping: true
                });
            }, 10)
        };
        $scope.run = function () {
            $scope.running = true;
            var sql = $scope.sql_editor.getValue();
            $http.post('/rest/system/sql/run', sql).success(function (data, status, headers, config) {
                $scope.runSuccess = data.flag;
                $scope.sql_result = data.msg;
                $scope.running = false;
            });
        };

        $scope.init();
    }]);
});